/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.postbar.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

/**
 *
 * @author wanglin
 */
public class SSLClientDemo {

    /**
     * 依赖关系：
     * <dependency>
     * <groupId>org.apache.httpcomponents</groupId>
     * <artifactId>httpclient</artifactId>
     * <version>4.5.2</version>
     * </dependency>
     */
    public static void main(String[] args) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
        SSLContextBuilder sslcontext = new SSLContextBuilder();
        //信任自签名的证书。该方法使得证书无需在ca服务器认证，可验证私有证书。
        sslcontext.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        CloseableHttpClient client = HttpClients.custom().setSSLContext(sslcontext.build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();

        /*
		 *
		 * 得到一个http client，后面和正常http client操作一样。
		 *
         */
        //get
        HttpGet get = new HttpGet("https://127.0.0.1:8121/s");
        CloseableHttpResponse response = client.execute(get);
        BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String str;
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }
        System.out.println();
        response.close();
        response = null;

        //post
        HttpPost post = new HttpPost("https://127.0.0.1:8121/postaaa");
        post.setEntity(new StringEntity("this is post method!"));
        response = client.execute(post);
        br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }
        System.out.println();
        response.close();
        response = null;

        //put
        HttpPut put = new HttpPut("https://127.0.0.1:8121/putbbb");
        put.setEntity(new StringEntity("haha, a put request appears ~"));
        response = client.execute(put);
        br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }
        System.out.println();
        response.close();
        response = null;
    }
}
